package com.lzh.persevere.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzh.persevere.mapper.ArticleInfoMapper;
import com.lzh.persevere.mapper.FanInfoMapper;
import com.lzh.persevere.model.entity.ArticleInfo;
import com.lzh.persevere.model.entity.FanInfo;
import com.lzh.persevere.model.vo.fanInfo.FanInfoVO;
import com.lzh.persevere.service.FanInfoService;
import com.lzh.persevere.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 粉丝表服务实现类
 *
 * @author luzhiheng
 * @since 2025-07-22
 */
@Service
public class FanInfoServiceImpl extends ServiceImpl<FanInfoMapper, FanInfo> implements FanInfoService {

    @Resource
    private RedisCacheService redisCacheService;

    @Resource
    private ArticleInfoMapper articleInfoMapper;

    @Override
    public List<FanInfoVO> findFanList() {
        // 关注列表
        List<FanInfo> followList = baseMapper.selectList(Wrappers.<FanInfo>lambdaQuery().eq(FanInfo::getOpenFlag, 1).eq(FanInfo::getCreateUserId, SecurityUtils.getUserId()));
        Map<String, Boolean> followMap = followList.stream().collect(Collectors.toMap(FanInfo::getFanId, e -> true, (k1, k2) -> k1));
        // 粉丝列表
        List<FanInfo> fanList = baseMapper.selectList(Wrappers.<FanInfo>lambdaQuery().eq(FanInfo::getOpenFlag, 1).eq(FanInfo::getFanId, SecurityUtils.getUserId()));

        Map<String, String> allUserIdNickNameMap = redisCacheService.getAllUserIdNickNameMap();
        Map<String, String> allUserIdAvatarMap = redisCacheService.getAllUserIdAvatarMap();

        List<FanInfoVO> fanInfoVOList = fanList.stream().map(e -> {
            FanInfoVO vo = new FanInfoVO();
            vo.setFanUserId(e.getCreateUserId());
            vo.setFanNickName(allUserIdNickNameMap.get(e.getCreateUserId()));
            vo.setFanUserAvatar(allUserIdAvatarMap.get(e.getCreateUserId()));
            vo.setIsMutualFollow(followMap.containsKey(e.getCreateUserId()));
            return vo;
        }).collect(Collectors.toList());
        return fanInfoVOList;
    }

    @Override
    public List<FanInfoVO> findFollowList() {
        // 关注列表
        List<FanInfo> followList = baseMapper.selectList(Wrappers.<FanInfo>lambdaQuery().eq(FanInfo::getOpenFlag, 1).eq(FanInfo::getCreateUserId, SecurityUtils.getUserId()));
        // 粉丝列表
        List<FanInfo> fanList = baseMapper.selectList(Wrappers.<FanInfo>lambdaQuery().eq(FanInfo::getOpenFlag, 1).eq(FanInfo::getFanId, SecurityUtils.getUserId()));
        Map<String, Boolean> fanMap = fanList.stream().collect(Collectors.toMap(FanInfo::getCreateUserId, e -> true, (k1, k2) -> k1));

        Map<String, String> allUserIdNickNameMap = redisCacheService.getAllUserIdNickNameMap();
        Map<String, String> allUserIdAvatarMap = redisCacheService.getAllUserIdAvatarMap();

        List<FanInfoVO> fanInfoVOList = followList.stream().map(e -> {
            FanInfoVO vo = new FanInfoVO();
            vo.setFanUserId(e.getCreateUserId());
            vo.setFanNickName(allUserIdNickNameMap.get(e.getFanId()));
            vo.setFanUserAvatar(allUserIdAvatarMap.get(e.getFanId()));
            vo.setIsMutualFollow(fanMap.containsKey(e.getFanId()));
            return vo;
        }).collect(Collectors.toList());
        return fanInfoVOList;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean followOrCancel(String userId, Integer status) {
        baseMapper.cancelFollow(SecurityUtils.getUserId(), userId);
        if (status == 1) {
            // 关注
            FanInfo fanInfo = new FanInfo();
            fanInfo.setCreateUserId(SecurityUtils.getUserId() + "");
            fanInfo.setFanId(userId);
            fanInfo.setOpenFlag(1);
            baseMapper.insert(fanInfo);
        }
        return true;
    }

    @Override
    public Map<String, Object> findCreateInfo() {
        Long userId = SecurityUtils.getUserId();
        Map<String, Object> resultMap = new HashMap<>();
        Long fanCount = baseMapper.selectCount(Wrappers.<FanInfo>lambdaQuery().eq(FanInfo::getFanId, userId).eq(FanInfo::getOpenFlag, 1));
        List<ArticleInfo> articleInfoList = articleInfoMapper.selectList(Wrappers.<ArticleInfo>lambdaQuery().eq(ArticleInfo::getCreateUserId, userId).eq(ArticleInfo::getOpenFlag, 1));
        int articleCount = 0;
        int viewCount = 0;
        int likeCount = 0;
        int collectCount = 0;
        int wordCount = 0;
        for (ArticleInfo articleInfo : articleInfoList) {
            viewCount += articleInfo.getViewCount();
            likeCount += articleInfo.getLikeCount();
            collectCount += articleInfo.getCollectCount();
            wordCount += articleInfo.getWordCount();
            articleCount++;
        }
        Map<String, String> allUserIdNickNameMap = redisCacheService.getAllUserIdNickNameMap();
        Map<String, String> allUserIdAvatarMap = redisCacheService.getAllUserIdAvatarMap();
        resultMap.put("avatar", allUserIdAvatarMap.get(userId + ""));
        resultMap.put("nickName", allUserIdNickNameMap.get(userId + ""));
        resultMap.put("fanCount", fanCount);
        resultMap.put("articleCount", articleCount);
        resultMap.put("viewCount", viewCount);
        resultMap.put("likeCount", likeCount);
        resultMap.put("collectCount", collectCount);
        resultMap.put("wordCount", wordCount);
        return resultMap;
    }
}
